package vn.bk.hotel;

import com.actionbarsherlock.app.SherlockFragment;

import vn.bk.hotel.R;
import vn.bk.hotel.dialog.ChooseTypeHotelLocation;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SearchByLocation extends SherlockFragment implements
	OnClickListener {

    private static boolean starEnabled = true;//
    private float distance = 1;//

    private static Button typeLocation;
    private static String[] typeText;//
    private Button distanceButton;
    private Button search;
    private static String[] allUriType;//
    private static String myUri;//

    private static final float DEFAULT_DISTANCE = 5;

    private static ImageView addRate, minusRate;
    private static TextView textNumRate;
    private static int numRate = 0;

    static LinearLayout layoutRate;
    static Bundle b;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	Log.i("Fragment", "On Create");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	View v = inflater.inflate(R.layout.tab_location, container, false);
	// rating = (RatingBar) v.findViewById(R.id.ratingbarLocation);
	addRate = (ImageView) v.findViewById(R.id.img_increase_rate);
	minusRate = (ImageView) v.findViewById(R.id.img_decrease_rate);
	textNumRate = (TextView) v.findViewById(R.id.tv_num_rate);
	typeLocation = (Button) v.findViewById(R.id.type_location_spinner);
	layoutRate = (LinearLayout) v.findViewById(R.id.layout_rate);
	typeText = getResources().getStringArray(R.array.type);
	distanceButton = (Button) v.findViewById(R.id.bt_number_distance);
	search = (Button) v.findViewById(R.id.bt_search_location);
	allUriType = getResources().getStringArray(R.array.type_uri);
	myUri = allUriType[0];
	distance = DEFAULT_DISTANCE;
	Log.i("Load Tab location", "Loaded");
	init();
	b = savedInstanceState;
	return v;
    }

    @Override
    public void onResume() {
	// TODO Auto-generated method stub
	Log.i("Search By Location", "Resume");
	super.onResume();
	if(b != null){
	    onCreate(b);
	}
    }
    
    

    @Override
    public void onDestroy() {
	// TODO Auto-generated method stub
	super.onDestroy();
	Log.i("Search By Location", "Destroy");
    }

    @Override
    public void onPause() {
	// TODO Auto-generated method stub
	super.onPause();
	Log.i("Search By Location", "Pause");
    }

    private void init() {
	distanceButton.setText(String.valueOf(distance) + " km");

	addRate.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		if (numRate < 5 && starEnabled) {
		    numRate++;
		    textNumRate.setText(String.valueOf(numRate));
		    minusRate.setImageResource(R.drawable.minus_rating);
		    if (numRate == 5) {
			addRate.setImageResource(R.drawable.up_disable);
		    }
		}
	    }
	});
	minusRate.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		// TODO Auto-generated method stub
		if (numRate > 0 && starEnabled) {
		    addRate.setImageResource(R.drawable.add_rating);
		    numRate--;
		    textNumRate.setText(numRate == 0 ? "All" : String
			    .valueOf(numRate));
		    if (numRate == 0) {
			minusRate
				.setImageResource(R.drawable.down_disable);
		    }
		}
	    }
	});
	// plus.setOnClickListener(this);
	// minus.setOnClickListener(this);
	// rating.setOnTouchListener(new OnTouchListener() {
	//
	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// // TODO Auto-generated method stub
	// float touchPositionX = event.getX();
	// float width = rating.getWidth();
	// float starsf = (touchPositionX / width) * 5.0f;
	// int stars = (int) starsf + 1;
	// rating.setRating(stars);
	// if (stars != 0) {
	// myUri = allUriType[1];
	// }
	// return true;
	//
	// }
	// });

	// rating.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
	//
	// @Override
	// public void onRatingChanged(RatingBar ratingBar, float rating,
	// boolean fromUser) {
	// // TODO Auto-generated method stub
	// if (rating != 0) {
	// myUri = allUriType[1];
	// typeLocation.setText(typeText[1]);
	// }
	//
	// }
	// });

	distanceButton.setOnClickListener(this);
	search.setOnClickListener(this);
	registerForContextMenu(distanceButton);
	changeTypeHotel(0);
	// rating.setEnabled(true);

	typeLocation.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		// TODO Auto-generated method stub
		new ChooseTypeHotelLocation(getActivity()).show();
	    }
	});

    }

    private static void disableNumRate() {

	addRate.setImageResource(R.drawable.up_disable);
	minusRate.setImageResource(R.drawable.down_disable);
	layoutRate.setVisibility(LinearLayout.GONE);

    }

    private static void enableNumRate() {
	addRate.setImageResource(R.drawable.add_rating);
	minusRate.setImageResource(R.drawable.minus_rating);
	layoutRate.setVisibility(LinearLayout.VISIBLE);
    }

    public static void changeTypeHotel(int paramInt) {
	myUri = allUriType[paramInt];
	typeLocation.setText(typeText[paramInt]);
	if (paramInt == 1) {
	    starEnabled = true;
	    textNumRate.setText(numRate == 0 ? "All" : String.valueOf(numRate));

	} else {
	    starEnabled = false;
	    textNumRate.setText("All");
	}
	if (starEnabled) {
	    enableNumRate();
	} else {
	    disableNumRate();
	}
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
	// TODO Auto-generated method stub
	String num = (String) item.getTitle();
	int n = num.indexOf("km");
	num = num.substring(0, n);
	distance = (Float.parseFloat(num));
	distanceButton.setText(String.valueOf(distance) + " km");
	return super.onContextItemSelected(item);

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
	    ContextMenuInfo menuInfo) {
	// TODO Auto-generated method stub
	super.onCreateContextMenu(menu, v, menuInfo);
	menu.setHeaderTitle(getString(R.string.distance));
	String[] arrayDistance = getResources()
		.getStringArray(R.array.distance);
	for (int i = 0; i < arrayDistance.length; i++) {
	    menu.add(0, v.getId(), 0, arrayDistance[i] + " km");
	}

    }

    @Override
    public void onClick(View v) {
	// TODO Auto-generated method stub
	switch (v.getId()) {

	case R.id.bt_number_distance:
	    distanceButton.showContextMenu();
	    break;
	case R.id.bt_search_location:
	    ListHotel.typeSearch = ListHotel.SEARCH_BY_LOCATION;
	    ListHotel.distance = distance;
	    ListHotel.numberStar = numRate;
	    ListHotel.uriType = myUri;

	    ListHotel.keyWord = "";

	    Log.i("distance", String.valueOf(ListHotel.distance) + " km");
	    Log.i("star", String.valueOf(ListHotel.numberStar));
	    Log.i("uri", ListHotel.uriType);
	    Log.i("keyword", ListHotel.keyWord);

	    getActivity().startActivity(
		    new Intent(getActivity(), ListHotel.class));

	    break;
	}
    }

}
